package com.ohuang.myapp;


import com.google.gson.Gson;
import com.ohuang.baseapp.utils.HiLogUtil;
import com.ohuang.myapp.bean.ChickenSoup;
import com.ohuang.myapp.slice.MainAbilitySlice;
import ohos.aafwk.ability.*;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.ComponentProvider;
import ohos.bundle.AbilityInfo;
import ohos.utils.PacMap;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class MainAbility extends Ability {
    private static final int DEFAULT_DIMENSION_2X2 = 2;
    public static final String url_ChickenSoup = "https://api.btstu.cn/yan/api.php?charset=utf-8&encode=json";
    public static String title = "标题";
    public static String content = "没有数据哦";


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        HiLogUtil.error("Oh","onStart");

    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);
    }

    @Override
    protected ProviderFormInfo onCreateForm(Intent intent) {
        HiLogUtil.error("Oh","onCreateForm");
        IntentParams params = intent.getParams();
        if (params == null) {
            return null;
        }

        String formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
        int dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);
        getData();
        if (formName.equals("service_widget")) {
            long id = (long) params.getParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY);
            if (dimension == DEFAULT_DIMENSION_2X2) {
                ProviderFormInfo providerFormInfo = new ProviderFormInfo(ResourceTable.Layout_form_image_with_info_service_widget_2_2, this);
                ComponentProvider componentProvider = providerFormInfo.getComponentProvider();
                componentProvider.setText(ResourceTable.Id_text_title, title);
                componentProvider.setText(ResourceTable.Id_text_content, content);
       
                providerFormInfo.mergeActions(componentProvider);
                return providerFormInfo;
            }
        }

        return null;
    }

    private void getData() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder().url(url_ChickenSoup).get().build();
                try {
                    Response response = okHttpClient.newCall(request).execute();
                    assert response.body() != null;
                    String json = response.body().string();
                    ChickenSoup c = new Gson().fromJson(json, ChickenSoup.class);

                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

                    content = c.getText();
                    title = sdf.format(new Date(System.currentTimeMillis())); // 时间戳转换日期
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    @Override
    protected void onUpdateForm(long formId) {
        super.onUpdateForm(formId);

        getData();
        ComponentProvider componentProvider = new ComponentProvider(ResourceTable.Layout_form_image_with_info_service_widget_2_2, this);
        componentProvider.setText(ResourceTable.Id_text_title, title);
        componentProvider.setText(ResourceTable.Id_text_content, content);
        try {
            updateForm(formId, componentProvider);
        } catch (FormException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onDeleteForm(long formId) {
        super.onDeleteForm(formId);

    }

    @Override
    protected void onCastTempForm(long formId) {
        super.onCastTempForm(formId);


    }

    @Override
    protected void onEventNotify(Map<Long, Integer> formEvents) {
        super.onEventNotify(formEvents);

    }

//    @Override
//    protected ProviderFormInfo onCreateForm(Intent intent) {
//        String formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
//        int dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);
//        if (formName.equals("service_widget")) {
//            if (dimension == DEFAULT_DIMENSION_2X2) {
//                return new ProviderFormInfo(ResourceTable.Layout_form_image_with_info_service_widget_2_2, this);
//            }
//        }
//        return null;
//    }




}
